package com.scalar.chiptrack.finance.openpo.actioncommands;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by KumaraSwamy.
 * Date: Mar 28, 2005
 * Time: 12:03:12 PM
 * To Save Package Types
 */
public class PurchaseOrderSavePackageTypeCommand extends ChipTrackExtendedAction
{

    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {

        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        PurchaseOrderForm purchaseOrderForm = ( PurchaseOrderForm ) form;
		String actionForward 		= "Search";
        HttpSession mSession 		= request.getSession();

		try
        {
            String[] packageType      = request.getParameterValues("packageType");

            if( packageType != null )
            {
                int packageTypeSize = 0;
                try
                {
                    packageTypeSize =  packageType.length;
                }
                catch (Exception ex)
                {
                    packageTypeSize = 0;
                }
                ArrayList packageTypeList = new ArrayList(packageTypeSize);

                for(int count = 0; count < packageTypeSize; count++ )
                {
                   if( ( packageType[ count ] != null ) && ( !( "".equalsIgnoreCase( packageType[ count ].trim() ) ) ) )
                   {
                       packageTypeList.add( packageType[ count ] );
                   }
                }

                PurchaseOrderDAO.savePakageType( null,  packageTypeList );
                info.put ( INFO_PACKAGE_TYPE_SAVED_SUCCEFULLY, null );
                Logger.info( INFO_PACKAGE_TYPE_SAVED_SUCCEFULLY );
            }

            request.setAttribute( "packageTypeList", PurchaseOrderDAO.loadPackTypeList() );

            actionForward = "purchaseOrderAddNewPackageType";
       	}
        catch ( Exception e )
        {
            Logger.error( ERROR_SAVING_PACKAGE_TYPE_INFORMATION, e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_SAVING_PACKAGE_TYPE_INFORMATION );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERROR_SAVING_PACKAGE_TYPE_INFORMATION, e ) );
            actionForward = "purchaseOrderAddNewPackageType";
        }

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
    }
}

